<?php
namespace app\index\controller;

use app\admin\model\LuckList;
use app\admin\model\OrderDraft;
use app\admin\model\share\CustomShare;
use app\common\controller\Frontend;
use app\common\library\EnterpriseWeChat;
use think\Exception;
use think\Validate;
use fast\Jssdk;
use think\Db;

class Share extends Frontend
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    public $wechat_name = '直采人';
    function dump($var, $echo=true, $label=null, $strict=true) {
        $label = ($label === null) ? '' : rtrim($label) . ' ';
        if (!$strict) {
            if (ini_get('html_errors')) {
                $output = print_r($var, true);
                $output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>';
            } else {
                $output = $label . print_r($var, true);
            }
        } else {
            ob_start();
            var_dump($var);
            $output = ob_get_clean();
            if (!extension_loaded('xdebug')) {
                $output = preg_replace('/\]\=\>\n(\s+)/m', '] => ', $output);
                $output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>';
            }
        }
        if ($echo) {
            echo($output);
            return null;
        }else
            return $output;
    }
    public function index(){
        $WeChatAppId = \think\Config::get('weixinConfig');
        $appId = $WeChatAppId[$this->wechat_name]['appId'];
        $appsecret = $WeChatAppId[$this->wechat_name]['appsecret'];
        $file = $WeChatAppId[$this->wechat_name]['filename'];
        $id = $this->request->get('id');
        $msg = CustomShare::where('id',$id)->find()->toArray();

        if(!$msg['switch'] || strtotime($msg['visa']) < time()){
            $this->error('信息失效');
            return false;
        }
        $msg['image'] = "https://crm.jiajiachengpin.com".$msg['image'];
        $jssdk = new Jssdk($appId, $appsecret);//去微信公众号里面找

        $msg['appId'] = $appId;
        $msg['appsecret'] = $appsecret;
        $signPackage = $jssdk->GetSignPackage($file);//取得签名及其他必要信息的信息包
        $this->assign('msg',$msg);
        $this->assign('signPackage',$signPackage);
        return $this->fetch();
    }

    /**
     * 草稿单
     */
    public function order_draft(){
        $WeChatAppId = \think\Config::get('weixinConfig');
        $appId = $WeChatAppId[$this->wechat_name]['appId'];
        $appsecret = $WeChatAppId[$this->wechat_name]['appsecret'];
        $file = $WeChatAppId[$this->wechat_name]['filename'];
        $msg['wechat'] = $this->wechat_name;
        $id = $this->request->get('id');
        $uid = $this->request->get('uid');
        $wid = $this->request->get('wid');
        $res['qrcode_id'] = $this->request->get('qrcode_id');
        $res = OrderDraft::where('id',$id)->find()->toArray();
        $draft_products = Db::name('draft_products')->where('draft_id',$id)->find();
        $res['product'] = json_decode($draft_products['products'],true);
        $m9 = Db::connect('M9DbConfig');
        if($res['product']){
            foreach ($res['product'] as $k => $v){
                $res['product'][$k]['productName'] = '';
                $p_name = $m9->table('product')->where('id',$v['id'])->value('name');
                $res['product'][$k]['productName'] = iconv('gbk', 'utf-8', $p_name);
            }
        }
        $res['appid'] = $appId;
        $res['appsecret'] = $appsecret;
        $res['uid'] = $uid;

        $friends = Db::name('wechat_friends_list')->where(['id'=>$wid])->find();
        if(!$friends){
            $this->error('微信好友获取失败');
        }
        $record = Db::name('wechat_record_list')->where(array('wechat_record_id'=>$friends['wechat_record_id']))->find();
        $customer = Db::name('wechat_customer_list')->where(array('account_id'=>$friends['account_id']))->find();
        $res['account_id'] = $customer['account_id'];
        $res['account_name'] = $customer['user_name'].'-'.$customer['nickname'];
        $res['record_id'] = $record['id'];
        $we_id = $record['wechat_alias'] ? $record['wechat_alias'] : $record['wechat_id'];
        $res['record_name'] = $record['nickname'].'('.$we_id.')';
        $res['friends_id'] = $friends['id'];
        $we_id = $friends['wechat_alias'] ? $friends['wechat_alias'] : $friends['wechat_id'];
        $res['friends_name'] = $friends['nickname'].'('.$we_id.')';
        $res['invoice'] = $record['device_num'];

        $this->assign('data',$res);
        $msg['title'] = '请补充订单个人信息';
        $msg['desc'] = $res['name'];
        $msg['image'] = '';
        $msg['link2'] = "https://crm.jiajiachengpin.com/index/share/order_draft?id=".$id;

        $jssdk = new Jssdk($appId, $appsecret);//去微信公众号里面找

        $signPackage = $jssdk->GetSignPackage($file);//取得签名及其他必要信息的信息包
        $this->assign('msg',$msg);
        $this->assign('signPackage',$signPackage);
        return $this->fetch();
    }

    /**
     * 抽奖页面
     */
    public function luck_draw(){
        $id = $this->request->get('id');
        $WeChatAppId = \think\Config::get('weixinConfig');
        $list = (new LuckList())->where('id',$id)->find();
        $appId = $WeChatAppId[$list['wechat']]['appId'];
        $appsecret = $WeChatAppId[$list['wechat']]['appsecret'];
        $file = $WeChatAppId[$list['wechat']]['filename'];
        $msg['wechat'] = $list['wechat'];
        $msg['id'] = $id;
        $msg['name'] = $list['name'];
        $msg['describe'] = $list['describe'];
        $msg['type'] = $list['type'];
        $msg['star_time'] = $list['star_time'];
        $msg['end_time'] = $list['end_time'];
        if(time() > $msg['end_time'])
        {
            $this->error('活动已结束');
        }
        if(time() < $msg['star_time'])
        {
            $this->error('活动未开始');
        }

        $msg['rule_image'] = $list['rule_image'];
        $msg['bg_image'] = $list['bg_image'];
        $msg['bg_music'] = $list['bg_music'];

        $products = Db::name('luck_product')->where('luck_id',$id)->field('id,name,left_over')->select();
        $msg['product'] = $products;

        $msg['title'] = $list['name'];
        $msg['desc'] = $list['describe'];
        $msg['image'] = "https://crm.jiajiachengpin.com".$list['image'];
        $msg['link'] = $list['link'];
        $msg['link2'] = $list['link2'];
        $msg['appid'] = $appId;
        $msg['appsecret'] = $appsecret;
        $this->assign('msg',$msg);

        //当前活动中奖纪录
        $log = Db::name('luck_log')->where('luck_id',$id)->field('user_name,product_name,time')->order('time desc')->select();
        foreach ($log as $logk => $logv) {
            $log[$logk]['time'] = date('Y-m-d H:i:s',$logv['time']);
        }
        $this->assign('log',$log);

        $jssdk = new Jssdk($appId,$appsecret);

        $signPackage = $jssdk->GetSignPackage($file);//取得签名及其他必要信息的信息包
        $this->assign('signPackage',$signPackage);
        return $this->fetch();
    }

    /**
     * 花名
     */
    public function roster(){
        $appId = 'wx4215cdf07d91ae0e';
        $appsecret = '1663701e37564817dbe52d62484e3ffc';
        $file = 'LR';
        $jssdk = new Jssdk($appId, $appsecret);//去微信公众号里面找

        $signPackage = $jssdk->GetSignPackage($file);//取得签名及其他必要信息的信息包

        print_r($signPackage);

        $msg['title'] = "XX";
        $msg['desc'] = "XX";
        $msg['link'] = "http://lr.huanghaipingsite.top/index/share/roster";
        $msg['link2'] = "http://lr.huanghaipingsite.top/index/share/roster";
        $msg['image'] = "http://local.jiajia.com/assets/img/bg/logo.png";

        $this->assign('msg',$msg);
        $this->assign('signPackage',$signPackage);
        return $this->fetch();
    }
    /**
     * 会员
     */
    public function member(){
        $WeChatAppId = \think\Config::get('weixinConfig');
        $appId = $WeChatAppId[$this->wechat_name]['appId'];
        $this->assign('msg',['appId' => $appId,'wechat' => '直采人']);
        return $this->fetch();
    }
    /**
     * 会刊
     */
    public function journal(){
        return $this->fetch();
    }
    public function upDown(){
        $group_id = $this->request->param('group_id');

        //(new EnterpriseWeChat())->EnterpriseWechatPushMessage('推广小程序','访问小程序的group_id：'.$group_id,[],['XiongKeQuan'],'https://crm.jiajiachengpin.com/');

        // $msg = DB::name('custom_share')->where('id',$id)->find();

//        if(!$msg['switch'] || strtotime($msg['visa']) < time()){
//            $this->error('信息失效');
//        }

        $wechat_push_info = Db::name('wechat_group')->where('id',$group_id)->value('wechat_push_info');
        $wechat_push_info = json_decode($wechat_push_info,true);
        $list = [];
        foreach($wechat_push_info as $k=>$v){
            if(!empty($v['weight']) && !empty($v['status'])){   //有权重和推广状态为1
                for($i = 0;$i<$v['weight'];$i++){
                    $list[] = $k;
                }
            }
        }

        $arr_key = array_rand($list,1);
        $wechat_id = $list[$arr_key];
        $wechat_info = Db::name('wechat_record_list')->where('id',$wechat_id)->field('qr_code,wechat_alias wechat')->find();

        $wechat_info['wxtgbg'] = 'https://crm.jiajiachengpin.com/static/img/wxtgbg.gif';
        $wechat_info['wxsq'] = 'https://crm.jiajiachengpin.com/static/img/wxsq.jpg';

        $this->assign('wechat_info',$wechat_info);
        return $this->fetch();
    }
}
?>

